#!./common/bats/bin/bats 
# -*-sh-*-

source ./common/test_helper_functions.bash || exit 1
source ./common/functions                  || exit 1

if [ -s ./common/TEST_ENV ];then
    source ./common/TEST_ENV
fi

PKG=DIMEMAS
testname="Dimemas"
module=dimemas
header=extern_comm_model.h
rpm=dimemas-$LMOD_FAMILY_COMPILER-$LMOD_FAMILY_MPI${DELIM}

setup() {
    module purge
    module load ohpc
    module load $module
}

@test "[$testname] Verify $module module is loaded and matches rpm version ($LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    module list $module | grep "1) $module" >& .cmd_output || exit 1
    run grep $module .cmd_output 
    assert_success
    
    # check version against rpm
    local version="$(rpm -q --queryformat='%{VERSION}\n' $rpm)"
    run cat .cmd_output
    assert_output "  1) $module/$version"
}

@test "[$testname] Verify module ${PKG}_DIR is defined and exists ($LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    DIR=${PKG}_DIR
    if [ -z ${!DIR} ];then
        flunk "${PKG}_DIR directory not defined"
    fi
    
    if [ ! -d ${!DIR} || -z "${!DIR}" ];then
        flunk "directory ${!DIR} does not exist"
    fi 
}

# ----------
# Binaries
# ----------

@test "[$testname] Verify module ${PKG}_BIN is defined and exists ($LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    BIN=${PKG}_BIN
    if [ -z ${!BIN} ];then
        flunk "${PKG}_BIN directory not defined"
    fi
    
    if [ ! -d ${!BIN} || -z "${!BIN}" ];then
        flunk "directory ${!BIN} does not exist"
    fi 
}

@test "[$testname] Verify availability of prv2dim binary ($LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    run which prv2dim
    assert_success
}

@test "[$testname] Verify availability of Dimemas binary ($LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    run which Dimemas
    assert_success
}

# Run Dimemas
@test "[$testname] Convert Paraver trace file to Dimemas format ($LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {

    rm -f *.dim *.pcf

    run prv2dim lulesh2_27p.prv lulesh2_27p.dim
    assert_success

    run ls lulesh2_27p.dim
    assert_success

    run ls lulesh2_27p.pcf
    assert_success
}

@test "[$testname] Run Dimemas simulation ($LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {

    rm -f prediction.prv

    run Dimemas -S 32K -p prediction.prv MN.128.1ppn.cfg
    assert_success

    run ls prediction.prv
    assert_success
}

rm -f .cmd_output

